/**
 * Created by Robin on 2017/5/11.
 */

class GetWeekDate{
    constructor(time){
        let now = time ? new Date(time) : new Date(); //当前日期
        this.nowDayOfWeek = now.getDay(); //今天本周的第几天
        this.nowYear = now.getYear(); //当前年
        this.nowMonth = now.getMonth(); //月
        this.nowDay = now.getDate(); //日
        this.beginHour="09:00:00";
        this.endHour="23:59:59";
        this.nowYear += (this.nowYear < 2000) ? 1900 : 0; //
        this.nowDayOfWeek = this.nowDayOfWeek===0?7:this.nowDayOfWeek; // 如果是周日，就变成周七
    }
    date2str(x,y){
        //date2str(new Date(curTime),"yyyy-MM-dd")
        let z ={y:x.getFullYear(),M:x.getMonth()+1,d:x.getDate(),h:x.getHours(),m:x.getMinutes(),s:x.getSeconds()};
        return y.replace(/(y+|M+|d+|h+|m+|s+)/g,function(v) {return ((v.length>1?"0":"")+eval('z.'+v.slice(-1))).slice(-(v.length>2?v.length:2))});
    }
    static formatDate(date){
        let myYear = date.getFullYear();
        let myMonth = date.getMonth()+1;
        let myWeekday = date.getDate();
        //alert("formatDate"+myYear+":"+myMonth+":"+myWeekday)
        if(myMonth < 10){
            myMonth = "0" + myMonth;
        }
        if(myWeekday < 10){
            myWeekday = "0" + myWeekday;
        }
        return (myYear+"-"+myMonth + "-" + myWeekday);
    }
    getWeekStartDate(isInt){
        //获得本周的开端日期
        let weekStartDate = new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek+1);
        if(isInt){
            return weekStartDate.getTime();
        }
        return GetWeekDate.formatDate(weekStartDate);
    }
    getWeekEndDate(){
        //获得本周的停止日期
        let weekEndDate = new Date(this.nowYear, this.nowMonth, this.nowDay + (6 - this.nowDayOfWeek+1));
        return GetWeekDate.formatDate(weekEndDate);
    }
    getLastWeekStartDate(isInt){
        //获得本周的开端日期
        let weekStartDate = new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek - 6);
        if(isInt){
            return weekStartDate.getTime();
        }
        return GetWeekDate.formatDate(weekStartDate);
    }
    getLastWeekEndDate(isInt){
        //获得本周的停止日期
        let weekEndDate = new Date(this.nowYear, this.nowMonth, this.nowDay + (6 - this.nowDayOfWeek+1) - 7);
        if(isInt){
            return weekEndDate.getTime();
        }
        return GetWeekDate.formatDate(weekEndDate);
    }
    getAWeedkYMD(){
        //获得本周周一~周日的年月日
        let ymdArr=[];
        for (let i = 0; i < 7; i++) {
            ymdArr[i]=[];
            //ymdArr[i][0]=this.formatDate(new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek+i+1));
            ymdArr[i][0]=this.date2str(new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek+i+1),'yyyy-MM-dd');
            ymdArr[i][1]=this.date2str(new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek+i+1), 'MM月dd日');
        }

        return ymdArr;
    }
    getMonthStartDate(isInt){
        //获得本周的开端日期
        let monthStartDate = new Date(this.nowYear, this.nowMonth, 1);
        if(isInt){
            return monthStartDate.getTime();
        }
        return GetWeekDate.formatDate(monthStartDate);
    }
}
module.exports = GetWeekDate;